import Canvas from '../../../utils/canvas'

import {
    wxfileTobase64,
    wait
} from '../../../utils/util'

export default class {
    constructor(jobInfo, qrcode) {
        console.info(jobInfo, qrcode)
        this.jobInfo = jobInfo
        this.qrcode = qrcode

        this.saveQrCode()
    }

    getImageInfo(src) {
        return new Promise(resolve => {
            wx.getImageInfo({
                src: src,
                success: (res => {
                    resolve(res)
                })
            })
        })
    }

    async saveQrCode() {
        const canvasWidth = 287
        const canvasHeight = 429

        const header = "/packageRecruit/template/share/assets/header.png"
        const headerSrc = await this.getImageInfo(header)
        const headerWidth = headerSrc.width / 2,
            headerHeight = headerSrc.height / 2

        const footer = "/packageRecruit/template/share/assets/footer.png"
        const footerSrc = await this.getImageInfo(footer)
        const footerWidth = footerSrc.width / 2,
            footerHeight = footerSrc.height / 2

        const company = "/packageRecruit/template/share/assets/company-icon.png"
        const companySrc = await this.getImageInfo(company)
        const companyWidth = companySrc.width / 2,
            companyHeight = companySrc.height / 2

        const jobInfo = this.jobInfo

        wx.showLoading({
            mask: true,
            title: '正在渲染中...',
            complete(res) {
                console.log('res', res)
            }
        })
        const ctx = wx.createCanvasContext('myCanvas')
        const canvas = new Canvas(ctx)
        canvas.roundRectColor(0, 0, canvasWidth, canvasHeight, 32, '#ffffff')
        // 背景图
        ctx.fillStyle = '#ffffff'
        canvas.circleImage(0, 0, headerWidth, headerHeight, 12, header, [true, true, false, false])
        canvas.circleImage(0, canvasHeight - footerHeight, footerWidth, footerHeight, 12, footer, [false, false, true, true])

        // 内容文本宽度 
        let contentWidth = canvasWidth - 60
        // 岗位招聘标题
        // 先对标题进行分段换行
        ctx.font = 'italic bold 24px cursive'
        let chr = jobInfo.jobName
        let temp = "";
        let titleList = [];
        const titleLineHeight = 33.5
        for (let a = 0; a < chr.length; a++) {
            if (ctx.measureText(temp).width < contentWidth) {
                temp += chr[a];
            } else {
                a--; //这里添加了a-- 是为了防止字符丢失
                titleList.push(temp);
                temp = "";
            }
        }
        titleList.push(temp);
        const titleMaxLine = titleList.length < 2 ? titleList.length : 2
        for (let b = 0; b < titleMaxLine; b++) {
            if (b === 1) titleList[b] = titleList[b].substring(0, titleList[b].length - 1) + '…'
            canvas.fillText(titleList[b], 15, 73 + ((b + 1) * titleLineHeight), '#202536', 24, 'left', true)
        }
        let currentUsedHeight = 73 + (titleLineHeight * titleMaxLine)

        // 薪资 lineHeight:29 + marginTop: 11
        ctx.font = 'italic bold 29px cursive'
        canvas.fillText(jobInfo.salary + jobInfo.jobSalaryType, 15, currentUsedHeight + 29 + 11, '#FF7642', 29, 'left', true)
        currentUsedHeight += 29 + 11

        // 详情 lineHeight:19 + marginTop: 12.5
        contentWidth = canvasWidth - 40
        currentUsedHeight += 12.5
        ctx.font = 'italic normal 14px cursive'
        chr = jobInfo.jobDetail
        temp = "";
        let detailList = [];
        // 字行高
        const detailLineHeight = 19
        for (let x = 0; x < chr.length; x++) {
            chr[x] = "·  " + chr[x]
            for (let a = 0; a < chr[x].length; a++) {
                if (temp == "" && a > 0) {
                    chr[x] = chr[x].slice(0, a) + "   " + chr[x].slice(a);
                }
                if (ctx.measureText(temp).width < contentWidth) {
                    temp += chr[x][a];
                    if (a == chr[x].length - 1) {
                        detailList.push(temp);
                        temp = "";
                    }
                } else {
                    a--; //这里添加了a-- 是为了防止字符丢失
                    detailList.push(temp);
                    temp = "";
                }
            }
        }

        detailList.push(temp);
        const detailMaxLine = detailList.length < 5 ? detailList.length : 5
        for (let b = 0; b < detailMaxLine; b++) {
            if (b === (detailMaxLine - 1)) detailList[b] = '…'
            canvas.fillText(detailList[b], 15, currentUsedHeight + ((b + 1) * detailLineHeight), '#565868', 14, 'left', false)
        }
        // marginBottom: 17
        currentUsedHeight += 17 + (detailLineHeight * detailMaxLine)
        // 被派遣单位信息
        // 图标
        canvas.circleImage(15, 305, companyWidth, companyHeight, 12, company, [false, false, false, false])
        // 名称
        canvas.fillText(jobInfo.serveCompanyName, 58, 305 + 14, '#202536', 12, 'left', true)
        // 地址 y: top + 字号
        canvas.fillText(jobInfo.jobAddress, 58, 324 + 12, '#9497B1', 11, 'left', false)

        // 底部
        // 招聘日期
        const date = jobInfo.createTime.substring(5, 7) + "月" + jobInfo.createTime.substring(8, 10) + "日"
        canvas.fillText(date + "热招岗位", 15, 373 + 24, '#FFFFFF', 21, 'left', true)
        canvas.fillText("扫描二维码，解锁更多好工作", 15, 399 + 19, '#FFFFFF', 11, 'left', false)
        // 二维码
        canvas.circleRectColor(217, 366, 65, '#ffffff')
        canvas.drawRoundImage(221, 370, 100, this.qrcode, '#ffffff')
        ctx.draw(true)
        ctx.restore()
        await wait(100)
        wx.canvasToTempFilePath({
            x: 0,
            y: 0,
            width: canvasWidth,
            height: canvasHeight,
            canvasId: 'myCanvas',
            success: (res) => {
                // console.log(res, '转换成功', res)
                wxfileTobase64(res.tempFilePath, true).then(url => {
                    // console.info(url)
                    // console.log(url,'url')
                    // let arr = []
                    // arr.push({
                    //     path:res.tempFilePath,
                    //     src:url
                    // })
                    wx.hideLoading()

                    // console.log(res,'res')
                    // this.self.setData({
                    //     posterInfo: {
                    //         path: res.tempFilePath,
                    //         src: url
                    //     },  
                    // }, () => {
                    //     // console.log(this.self.data.posterInfo, 'posterInfo')
                    // })
                })
            },
            fail(err) {
                console.info(err)
            }
        })
        // }
    }

    get s() {
        return 111
    }
}